{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Wikipedia Search Tools\n",
    "\n",
    "AG2's Wikipedia search integration allows agents to perform searches in Wikipedia and retrieve the relevant pages. Follow these steps to integrate Wikipedia Search Tools with AG2 Agents.\n",
    "\n",
    "Two tools are available for your AG2 agents:\n",
    "- `WikipediaQueryRunTool` executes Wikipedia queries and returning summarized page results\n",
    "- `WikipediaPageLoadTool` loads the contents of a Wikipedia page together with its metadata (for detailed data extraction)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "## Package Installation\n",
    "\n",
    "\n",
    "To get started with the Wikipedia search integration in AG2, follow these steps:\n",
    "\n",
    "Install AG2 with `\"wikipedia` and `openai` since we use OpenAI's LLMs in our example:\n",
    "\n",
    "```bash\n",
    "pip install -U \"ag2[wikipedia, openai]\"\n",
    "```\n",
    "> **Note:** If you have been using `autogen` or `ag2`, all you need to do is upgrade it using:\n",
    "> ```bash\n",
    "> pip install -U \"autogen[wikipedia, openai]\"\n",
    "> ```\n",
    "> or\n",
    "> ```bash\n",
    "> pip install -U \"ag2[wikipedia, openai]\"\n",
    "> ```\n",
    "> as `autogen`, and `ag2` are aliases for the same PyPI package.\n",
    "\n",
    "\n",
    "You're all set! Now you can start using Wikipedia Search with AG2."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Implementation\n",
    "The `WikipediaQueryRunTool` enables agents to perform Wikipedia search."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from autogen import AssistantAgent, LLMConfig, UserProxyAgent\n",
    "from autogen.tools.experimental import WikipediaPageLoadTool, WikipediaQueryRunTool"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Agent Configuration\n",
    "\n",
    "Configure an assistant agent and user proxy to be used for LLM recommendation and execution respectively.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "config_list = LLMConfig(config_list={\"api_type\": \"openai\", \"model\": \"gpt-5-nano\"})\n",
    "\n",
    "assistant = AssistantAgent(\n",
    "    name=\"assistant\",\n",
    "    llm_config=config_list,\n",
    ")\n",
    "\n",
    "user_proxy = UserProxyAgent(name=\"user_proxy\", human_input_mode=\"NEVER\", code_execution_config=False)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Query Tool Setup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "wikipedia_query_tool = WikipediaQueryRunTool()\n",
    "\n",
    "# Register the tool for LLM recommendation (assistant agent) and execution (user_proxy agent).\n",
    "wikipedia_query_tool.register_for_llm(assistant)\n",
    "wikipedia_query_tool.register_for_execution(user_proxy)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Start the Conversation\n",
    "\n",
    "With the setup complete, you can now use the assistant to search Wikipedia."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "response = user_proxy.initiate_chat(\n",
    "    recipient=assistant,\n",
    "    message=\"Who is the father of AI?\",\n",
    "    max_turns=2,\n",
    ")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Page Load Tool Setup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Start by removing the Query tool so we ensure our agent uses the Page Load tool\n",
    "assistant.remove_tool_for_llm(wikipedia_query_tool)\n",
    "\n",
    "# Create the Page Load tool\n",
    "wikipedia_page_load_tool = WikipediaPageLoadTool()\n",
    "\n",
    "# Register the tool for LLM recommendation (assistant agent) and execution (user_proxy agent).\n",
    "wikipedia_page_load_tool.register_for_llm(assistant)\n",
    "wikipedia_page_load_tool.register_for_execution(user_proxy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "response = user_proxy.initiate_chat(\n",
    "    recipient=assistant,\n",
    "    message=\"What's the population of Australia?\",\n",
    "    max_turns=2,\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "front_matter": {
   "description": "Perplexity Search Integration with AG2",
   "tags": [
    "tools",
    "perplexity",
    "web-search",
    "search"
   ]
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.21"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
